### 0. Load the requird library
rm(list=ls())
library(haven)
library(dplyr)
library(ggeffects)
library(ggpubr)
library(ggplot2)
library(stargazer)
library(rstan)
library(dynlm)
options(mc.cores = parallel::detectCores())
gc()


load("Data/IRT model.RData")

extract_thetas <- function(model) {
  
  thetas <- summary(model, pars = "theta2", probs = c(.1, .5, .9))
  thetas <- as.data.frame(thetas)
  thetas$j <- rep(1:5)
  thetas$t <- rep(1:47, each=5)
  
  return(thetas)
}

thetas <- extract_thetas(model)

extract_betas <- function(model) {
  
  betas <- summary(model, pars = "beta", probs =  c(.05, .5, .95))
  betas <- as.data.frame(betas)
  betas$i <- rep(1:369)
  
  
  return(betas)
}

betas <- extract_betas(model)

#########################################################################################################
######################################### Analyses in the paper #########################################
#########################################################################################################

##### Figure 2 #####

order.plot <- order(betas$summary.50. * -1)

betas <- betas %>% 
  mutate(color = ifelse(summary.95.<0 | summary.5.>0, "black", "grey"))

png("Figure/Figure 2.png", width = 2500, height = 2500, res=300)
par(mar=c(5,5,2,2))
plot(NULL, xlim=c(min(betas$summary.95. *-1), max(betas$summary.5. * -1)), ylim = c(0,369),bty="n", yaxt="n", 
     ylab = "", xlab = expression("Discrimination parameter b"[i]))

for (i in 1:nrow(betas)) {
  points(x=betas[i,]$summary.50. *-1, y=which(i==order.plot), pch=19, col = betas[i,]$color)
  segments(x0 = betas[i,]$summary.5. *-1, x1=betas[i,]$summary.95. *-1, y0=which(i==order.plot), y1=which(i==order.plot), col = betas[i,]$color)
}

segments(x0=0, x1=0, y0=0, y1=364)
text(x = 5, y = 25, labels = "More conservative items")
text(x = -8, y = 330, labels = "More progressive items")
dev.off()


##### Creates dyadic data with different time lags

make_data_model <- function(model, num_year) {
  
  swissvotes <- read.csv(url("https://swissvotes.ch/page/dataset/swissvotes_dataset.csv"), sep = ";")
  
  
  swissvotes <- swissvotes[swissvotes$anr>=224 & ### After 1971
                             swissvotes$anr<=609 & ###Before 2017
                             swissvotes$rechtsform<=3, ## Only initivative, referenda mandatory and facultative
                           c(which(colnames(swissvotes)=="anr"),
                             which(colnames(swissvotes)=="datum"),
                             which(colnames(swissvotes)=="p.fdp"),
                             which(colnames(swissvotes)=="p.cvp"),
                             which(colnames(swissvotes)=="p.svp"),
                             which(colnames(swissvotes)=="p.gps"),
                             which(colnames(swissvotes)=="p.sps"),
                             which(colnames(swissvotes)=="rechtsform"))]
  
  for (i in 3:7) {
    if (i==3) {
      data <- swissvotes[,c(1, 2, 8)]
      data$pos <- swissvotes[,i]
      data$party <- substr(colnames(swissvotes)[i], 3, 5)
      data$year <- as.numeric(substr(swissvotes$datum, 7, 10))
    } else {
      dta <- swissvotes[,c(1, 2, 8)]
      dta$pos <- swissvotes[,i]
      dta$party <- substr(colnames(swissvotes)[i], 3, 5)
      dta$year <- as.numeric(substr(swissvotes$datum, 7, 10))
      data <- rbind(data, dta)
    }
  }
  
  
  data <- data %>% 
    mutate(i = as.numeric(as.factor(anr)),
           j = as.numeric(as.factor(party)),
           t = as.numeric(as.factor(year)),
           y = ifelse(pos==1, 1, ifelse(pos==2, 0, NA)))
  
  ## Merge the key for the survey data
  key <- read.csv("Data/Key identification to merge survey and observational data.csv", sep = ";")
  
  data <- merge(data, key, by = "anr", all.x=T)
  
  
  CAP <- read.csv(url("https://comparativeagendas.s3.amazonaws.com/datasetfiles/switzerland_direct_democracy.csv"), sep = ",")
  CAP$anr <- c(substr(CAP[1:582,]$source, 12, nchar(CAP[1:582,]$source)), substr(CAP[582:604,]$source, 5, 7))
  
  data <- merge(data, CAP, by = "anr", all.x=T)
  
  load("Data/Data for party voters.RData")
  
  betas <- summary(model, pars = "beta")
  betas <- as.data.frame(betas)
  betas$i <- 1:nrow(betas)
  
  data <- merge(data, betas, by = "i")
  data <- merge(data, party_vote_data, by = c("projetx", "party"), all.x = T)
  
  
  
  data <- data[is.na(data$projetx)==F,]
  
  data <- data[data$summary.50.>1 | data$summary.50.<(-1),]
  
  ## Gets project list to create the dyadic dataframe. 
  projects <- unique(data[as.Date(data$datum, "%d.%m.%Y")>"1982-01-01",]$projetx)
  
  ## Loop to create the dyadic dataframe --> minus 5 years. 
  for (i in projects) {
    if (i==projects[1]) {
      init_data <- data[data$projetx==i,]
      
      dateNum <- as.Date(data[data$projetx==i,]$datum, "%d.%m.%Y")
      
      dateNum5 <- dateNum - 365*num_year
      
      prev_data <- data[as.Date(data$datum,  "%d.%m.%Y")<dateNum &
                          as.Date(data$datum,  "%d.%m.%Y")>dateNum5,]
      
      whole_data <- merge(init_data, prev_data, by = "party")
      
    } else {
      init_data <- data[data$projetx==i,]
      
      dateNum <- as.Date(data[data$projetx==i,]$datum, "%d.%m.%Y")
      
      dateNum5 <- dateNum - 365*num_year
      
      prev_data <- data[as.Date(data$datum,  "%d.%m.%Y")<dateNum &
                          as.Date(data$datum,  "%d.%m.%Y")>dateNum5,]
      
      whole_dta <- merge(init_data, prev_data, by = "party")
      whole_data <- rbind(whole_data, whole_dta)
      
    }
    
  }
  
  whole_data <- whole_data %>% 
    mutate(same_dim = ifelse(majortopic.x==majortopic.y, 1, 0),
           diff_pos = ifelse(rec_bin.x==rec_bin.y, 0, ifelse(rec_bin.x!=rec_bin.y, 1, NA)),
           part_agree_vote = ifelse(rec_bin.y == 0, 1-mean_supp_part.y, ifelse(rec_bin.y==1, mean_supp_part.y, NA)),
           all_agree_vote = ifelse(rec_bin.y == 0, 1-mean_supp_all.y, ifelse(rec_bin.y==1, mean_supp_all.y, NA)),
           diff_part_all = part_agree_vote - all_agree_vote,
           aim.x = ifelse(summary.50..x>0, "Cons", ifelse(summary.50..x<0, "Prog", NA)),
           aim.y = ifelse(summary.50..y>0, "Cons", ifelse(summary.50..y<0, "Prog", NA)),
           prog.pos.x = ifelse(aim.x=="Prog" & rec_bin.x ==1, 1, 
                               ifelse(aim.x=="Cons" & rec_bin.x==0, 1, 
                                      ifelse(aim.x=="Prog" & rec_bin.x ==0, 0, 
                                             ifelse(aim.x=="Cons" & rec_bin.x==1, 0, NA)))),
           prog.pos.y = ifelse(aim.y=="Prog" & rec_bin.y ==1, 1, 
                               ifelse(aim.y=="Cons" & rec_bin.y==0, 1, 
                                      ifelse(aim.y=="Prog" & rec_bin.y ==0, 0, 
                                             ifelse(aim.y=="Cons" & rec_bin.y==1, 0, NA)))),
           prog.vote.median.y = ifelse(aim.y=="Prog", mean_supp_all.y,
                                       ifelse(aim.y=="Cons", 1 - mean_supp_all.y, NA)),
           prog.vote.partisan.y = ifelse(aim.y=="Prog", mean_supp_part.y,
                                         ifelse(aim.y=="Cons", 1 - mean_supp_part.y, NA)),
           prog.vote.relative.partisan.y = ifelse(aim.y=="Prog", mean_supp_part.y - mean_supp_all.y,
                                                  ifelse(aim.y=="Cons", (mean_supp_part.y - mean_supp_all.y) * -1, NA)))
  
  return(whole_data)
}

whole_data3 <- make_data_model(model, num_year = 3)
whole_data1 <- make_data_model(model, num_year = 1)
whole_data5 <- make_data_model(model, num_year = 5)


##### Models in order: 

##### Table 3 #####

model.median.prog <- glm(prog.pos.x ~ prog.pos.y + prog.vote.median.y, data = whole_data3, family = binomial(link="logit"))
summary(model.median.prog)

model.median.prog2 <- glm(prog.pos.x ~ prog.pos.y * prog.vote.median.y, data = whole_data3, family = binomial(link="logit"))
summary(model.median.prog2)

stargazer(model.median.prog, model.median.prog2, 
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(0.05, 0.01, 0.001), no.space = T)

##### Figure 3 #####
preds.median <- ggpredict(model.median.prog2, c("prog.vote.median.y[all]", "prog.pos.y"))

png("Figure/Figure 3.png", width=2500, height=3500, res = 300)
ggarrange(
  ggplot(preds.median)+
    geom_line(aes(x=x, y=predicted, color = group))+
    geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
    geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
    theme_minimal()+
    scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
    labs(color = "Party position \n in previous ballot")+
    xlab("Median voters' support for conservative position")+
    ylab("Probability of conservative position")+
    theme(legend.position = "bottom"),
  ggplot(whole_data3)+
    geom_histogram(mapping = aes(x=prog.vote.median.y), alpha = 0.5)+
    labs(title = "Distribution of observations")+
    xlab("Median voters' support for conservative position")+
    theme_minimal(),
  nrow=2
)
dev.off()


##### Table 4 #####

model.partisan.prog <- glm(prog.pos.x ~ prog.pos.y + prog.vote.partisan.y, data = whole_data3, family = binomial(link="logit"))
summary(model.partisan.prog)
plot(ggpredict(model.partisan.prog, c("prog.vote.partisan.y[all]")))

model.partisan.prog2 <- glm(prog.pos.x ~ prog.pos.y * prog.vote.partisan.y, data = whole_data3, family = binomial(link="logit"))
summary(model.partisan.prog)
plot(ggpredict(model.partisan.prog, c("prog.vote.partisan.y[all]")))


stargazer(model.partisan.prog, model.partisan.prog2, 
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(0.05, 0.01, 0.001), no.space = T)

##### Figure 4 #####
preds.partisan <- ggpredict(model.partisan.prog2, c("prog.vote.partisan.y[all]", "prog.pos.y"))

png("Figure/Figure 4.png", width=2500, height=3500, res = 300)
ggarrange(
  ggplot(preds.partisan)+
    geom_line(aes(x=x, y=predicted, color = group))+
    geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
    geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
    theme_minimal()+
    scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
    labs(color = "Party position \n in previous ballot")+
    xlab("partisan voters' support for conservative position")+
    ylab("Probability of conservative position")+
    theme(legend.position = "bottom"),
  ggplot(whole_data3)+
    geom_histogram(mapping = aes(x=prog.vote.partisan.y), alpha = 0.5)+
    labs(title = "Distribution of observations")+
    xlab("partisan voters' support for conservative position")+
    theme_minimal(),
  nrow=2
)
dev.off()

##### Table 5 #####

model.relative.partisan.prog <- glm(prog.pos.x ~ prog.pos.y + prog.vote.relative.partisan.y + prog.vote.median.y, data = whole_data3, family = binomial(link="logit"))
summary(model.relative.partisan.prog)
plot(ggpredict(model.relative.partisan.prog, c("prog.vote.relative.partisan.y[all]")))

model.relative.partisan.prog2 <- glm(prog.pos.x ~ prog.pos.y * prog.vote.relative.partisan.y + prog.vote.median.y, data = whole_data3, family = binomial(link="logit"))
summary(model.relative.partisan.prog)
plot(ggpredict(model.relative.partisan.prog2, c("prog.vote.relative.partisan.y[all]")))


stargazer(model.relative.partisan.prog, model.relative.partisan.prog2, 
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(0.05, 0.01, 0.001), no.space = T)

##### Figure 5 #####

preds.relative.partisan <- ggpredict(model.relative.partisan.prog2, c("prog.vote.relative.partisan.y[all]", "prog.pos.y"))

png("Figure/Figure 5.png", width=2500, height=3500, res = 300)
ggarrange(
  ggplot(preds.relative.partisan)+
    geom_line(aes(x=x, y=predicted, color = group))+
    geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
    geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
    theme_minimal()+
    scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
    labs(color = "Party position \n in previous ballot")+
    xlab("Relative partisan voters' support for conservative position")+
    ylab("Probability of conservative position")+
    theme(legend.position = "bottom"),
  ggplot(whole_data3)+
    geom_histogram(mapping = aes(x=prog.vote.relative.partisan.y), alpha = 0.5)+
    labs(title = "Distribution of observations")+
    xlab("Relative partisan voters' support for conservative position")+
    theme_minimal(),
  nrow=2
)
dev.off()

#########################################################################################################
######################################### Supplement materials ##########################################
#########################################################################################################

data_project <- whole_data1 %>% 
  group_by(party, projetx.y, anr.y, datum.y, prog.vote.relative.partisan.y, prog.pos.y, prog.vote.median.y, prog.vote.partisan.y) %>% 
  summarize()

data_project <- data_project %>% 
  mutate(clash_median = ifelse(prog.pos.y==0 & prog.vote.relative.partisan.y<0, "no clash", 
                               ifelse(prog.pos.y==1 & prog.vote.relative.partisan.y>0, "no clash", 
                                      ifelse(prog.pos.y==0 & prog.vote.relative.partisan.y>0, "clash", 
                                             ifelse(prog.pos.y==1 & prog.vote.relative.partisan.y<0, "clash", NA)))),
         status_quo = ifelse(prog.vote.median.y>.5 & prog.vote.partisan.y>.5, "Same", 
                             ifelse(prog.vote.median.y<.5 & prog.vote.partisan.y<.5, "Same", 
                                    ifelse(prog.vote.median.y<.5 & prog.vote.partisan.y>.5, "Diff", 
                                           ifelse(prog.vote.median.y>.5 & prog.vote.partisan.y<.5, "Diff", NA)))),
         allignment = ifelse(prog.pos.y==0 & prog.vote.relative.partisan.y<0, prog.vote.relative.partisan.y * -1, 
                             ifelse(prog.pos.y==1 & prog.vote.relative.partisan.y>0, prog.vote.relative.partisan.y, 
                                    ifelse(prog.pos.y==0 & prog.vote.relative.partisan.y>0, prog.vote.relative.partisan.y *-1, 
                                           ifelse(prog.pos.y==1 & prog.vote.relative.partisan.y<0, prog.vote.relative.partisan.y, NA)))))

#### Table A1 ####

table(data_project$clash_median, data_project$party)
table(data_project$status_quo, data_project$party)


####

swissvotes <- read.csv(url("https://swissvotes.ch/page/dataset/swissvotes_dataset.csv"), sep = ";")

swissvotes <- swissvotes[swissvotes$anr>=224 & ### After 1971
                           swissvotes$anr<=609 & ###Before 2017
                           swissvotes$rechtsform<=3, ## Only initivative, referenda mandatory and facultative
                         c(which(colnames(swissvotes)=="anr"),
                           which(colnames(swissvotes)=="datum"),
                           which(colnames(swissvotes)=="titel_kurz_f"),
                           which(colnames(swissvotes)=="p.fdp"),
                           which(colnames(swissvotes)=="p.cvp"),
                           which(colnames(swissvotes)=="p.svp"),
                           which(colnames(swissvotes)=="p.gps"),
                           which(colnames(swissvotes)=="p.sps"),
                           which(colnames(swissvotes)=="rechtsform"),
                           which(colnames(swissvotes)=="bet"))]


swissvotes$i <- as.numeric(as.factor(swissvotes$anr))

betas <- summary(model, pars = "beta")
betas <- as.data.frame(betas)
betas$i <- 1:nrow(betas)

swissvotes_betas <- merge(swissvotes, betas, by = "i")

swissvotes_betas <- swissvotes_betas %>% 
  mutate(aim.y = ifelse(summary.50.>1, "Cons", ifelse(summary.50.< (-1), "Prog", "none")))

##### Table A2

key <- read.csv("Data/Key identification to merge survey and observational data.csv", sep = ";")

table(swissvotes_betas[swissvotes_betas$anr %in% key$anr,]$aim.y, 
      swissvotes_betas[swissvotes_betas$anr %in% key$anr,]$rechtsform)

##### Serial autocorrelation

data_date <- data_project %>% 
  group_by(party, datum.y) %>% 
  summarize(allignment = mean(allignment, na.rm = T))


data_date$date <- as.Date(data_date$datum.y, "%d.%m.%Y")

data_date$date2 <- as.numeric(as.factor(data_date$date))



####### Figure A1 #######
png("Figure/Figure A1.png", width = 3000, height = 2000, res=300)
ggarrange(
  ggplot(data_date[data_date$party=="cvp",])+
    geom_line(aes(x=date, y = allignment))+
    theme_minimal()+
    xlab("Date")+
    ylab("Party voter relative alignment")+
    ylim(min(data_date$allignment, na.rm=T), max(data_date$allignment, na.rm=T))+
    geom_hline(yintercept=0, color = "red")+
    labs(subtitle = "CVP"),
  
  ggplot(data_date[data_date$party=="fdp",])+
    geom_line(aes(x=date, y = allignment))+
    theme_minimal()+
    xlab("Date")+
    ylab("Party voter relative alignment")+
    ylim(min(data_date$allignment, na.rm=T), max(data_date$allignment, na.rm=T))+
    geom_hline(yintercept=0, color = "red")+
    labs(subtitle = "FDP"),
  
  ggplot(data_date[data_date$party=="svp",])+
    geom_line(aes(x=date, y = allignment))+
    theme_minimal()+
    xlab("Date")+
    ylab("Party voter relative alignment")+
    ylim(min(data_date$allignment, na.rm=T), max(data_date$allignment, na.rm=T))+
    geom_hline(yintercept=0, color = "red")+
    labs(subtitle = "SVP"),
  
  ggplot(data_date[data_date$party=="sps",])+
    geom_line(aes(x=date, y = allignment))+
    theme_minimal()+
    xlab("Date")+
    ylab("Party voter relative alignment")+
    geom_hline(yintercept=0, color = "red")+
    ylim(min(data_date$allignment, na.rm=T), max(data_date$allignment, na.rm=T))+
    labs(subtitle = "SP"),
  
  ggplot(data_date[data_date$party=="gps",])+
    geom_line(aes(x=date, y = allignment))+
    theme_minimal()+
    xlab("Date")+
    ylab("Party voter relative alignment")+
    ylim(min(data_date$allignment, na.rm=T), max(data_date$allignment, na.rm=T))+
    geom_hline(yintercept=0, color = "red")+
    labs(subtitle = "GPS"), 
  nrow=2, ncol=3, align = "hv")
dev.off()


##### Autoregressive models #####
data_date2 <- data_date[order(data_date$date2),]
autoreg_cvp1 <- dynlm(ts(data_date2[data_date2$party=="cvp",]$allignment) ~ L(ts(data_date2[data_date2$party=="cvp",]$allignment)))
autoreg_cvp2 <- dynlm(ts(data_date2[data_date2$party=="cvp",]$allignment) ~ L(ts(data_date2[data_date2$party=="cvp",]$allignment)) + L(ts(data_date2[data_date2$party=="cvp",]$allignment), 2))
autoreg_fdp1 <- dynlm(ts(data_date2[data_date2$party=="fdp",]$allignment) ~ L(ts(data_date2[data_date2$party=="fdp",]$allignment)))
autoreg_fdp2 <- dynlm(ts(data_date2[data_date2$party=="fdp",]$allignment) ~ L(ts(data_date2[data_date2$party=="fdp",]$allignment)) + L(ts(data_date2[data_date2$party=="fdp",]$allignment), 2))
autoreg_svp1 <- dynlm(ts(data_date2[data_date2$party=="svp",]$allignment) ~ L(ts(data_date2[data_date2$party=="svp",]$allignment)))
autoreg_svp2 <- dynlm(ts(data_date2[data_date2$party=="svp",]$allignment) ~ L(ts(data_date2[data_date2$party=="svp",]$allignment)) + L(ts(data_date2[data_date2$party=="svp",]$allignment), 2))
autoreg_gps1 <- dynlm(ts(data_date2[data_date2$party=="gps",]$allignment) ~ L(ts(data_date2[data_date2$party=="gps",]$allignment)))
autoreg_gps2 <- dynlm(ts(data_date2[data_date2$party=="gps",]$allignment) ~ L(ts(data_date2[data_date2$party=="gps",]$allignment)) + L(ts(data_date2[data_date2$party=="gps",]$allignment), 2))
autoreg_sps1 <- dynlm(ts(data_date2[data_date2$party=="sps",]$allignment) ~ L(ts(data_date2[data_date2$party=="sps",]$allignment)))
autoreg_sps2 <- dynlm(ts(data_date2[data_date2$party=="sps",]$allignment) ~ L(ts(data_date2[data_date2$party=="sps",]$allignment)) + L(ts(data_date2[data_date2$party=="sps",]$allignment), 2))

model_list_autoreg1 <- list(autoreg_cvp1, autoreg_fdp1, autoreg_svp1, autoreg_sps1, autoreg_gps1)
model_list_autoreg2 <- list(autoreg_cvp2, autoreg_fdp2, autoreg_svp2, autoreg_sps2, autoreg_gps2)

### Table A3
stargazer(model_list_autoreg1)
### Table A4
stargazer(model_list_autoreg2)


########### Replication models with one and five year lages #########

#### Median Voter 

### 1 year
model.median.prog.1 <- glm(prog.pos.x ~ prog.pos.y + prog.vote.median.y, data = whole_data1, family = binomial(link="logit"))

model.median.prog2.1 <- glm(prog.pos.x ~ prog.pos.y * prog.vote.median.y, data = whole_data1, family = binomial(link="logit"))

### 5 years
model.median.prog.5 <- glm(prog.pos.x ~ prog.pos.y + prog.vote.median.y, data = whole_data5, family = binomial(link="logit"))

model.median.prog2.5 <- glm(prog.pos.x ~ prog.pos.y * prog.vote.median.y, data = whole_data5, family = binomial(link="logit"))


### List models
list_model_median.1.5 <- list(model.median.prog.1, model.median.prog2.1, model.median.prog.5, model.median.prog2.1)

#### Table A5 #####
stargazer(list_model_median.1.5, 
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(0.05, 0.01, 0.001), no.space = T)



##### Figure A2

preds.median.1 <- ggpredict(model.median.prog2.1, c("prog.vote.median.y[all]", "prog.pos.y"))


p1.median <- ggarrange(
  ggplot(preds.median.1)+
    geom_line(aes(x=x, y=predicted, color = group))+
    geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
    geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
    theme_minimal()+
    scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
    labs(color = "Party position \n in previous ballot")+
    xlab("Median voters' support for conservative position")+
    ylab("Probability of conservative position")+
    theme(legend.position = "bottom"),
  ggplot(whole_data1)+
    geom_histogram(mapping = aes(x=prog.vote.median.y), alpha = 0.5)+
    labs(title = "Distribution of observations")+
    xlab("Median voters' support for conservative position")+
    theme_minimal(),
  nrow=2
)


p1.median <- annotate_figure(p1.median, top = text_grob("One year lag", 
                                                        color = "black",size = 14))

preds.median.5 <- ggpredict(model.median.prog2.5, c("prog.vote.median.y[all]", "prog.pos.y"))


p5.median <- ggarrange(
  ggplot(preds.median.5)+
    geom_line(aes(x=x, y=predicted, color = group))+
    geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
    geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
    theme_minimal()+
    scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
    labs(color = "Party position \n in previous ballot")+
    xlab("Median voters' support for conservative position")+
    ylab("Probability of conservative position")+
    theme(legend.position = "bottom"),
  ggplot(whole_data5)+
    geom_histogram(mapping = aes(x=prog.vote.median.y), alpha = 0.5)+
    labs(title = "Distribution of observations")+
    xlab("Median voters' support for conservative position")+
    theme_minimal(),
  nrow=2
)

p5.median <- annotate_figure(p5.median, top = text_grob("Five year lag", 
                                                        color = "black",size = 14))
png("Figure/Figure A2.png", width=3000, height=2000, res=300)
ggarrange(p1.median, p5.median, ncol=2)
dev.off()


#### Partisan Voter 

### 1 year

model.partisan.prog.1 <- glm(prog.pos.x ~ prog.pos.y + prog.vote.partisan.y, data = whole_data1, family = binomial(link="logit"))

model.partisan.prog2.1 <- glm(prog.pos.x ~ prog.pos.y * prog.vote.partisan.y, data = whole_data1, family = binomial(link="logit"))

### 5 years
model.partisan.prog.5 <- glm(prog.pos.x ~ prog.pos.y + prog.vote.partisan.y, data = whole_data5, family = binomial(link="logit"))

model.partisan.prog2.5 <- glm(prog.pos.x ~ prog.pos.y * prog.vote.partisan.y, data = whole_data5, family = binomial(link="logit"))

### List models
list_model_partisan.1.5 <- list(model.partisan.prog.1, model.partisan.prog2.1, model.partisan.prog.5, model.partisan.prog2.1)

### Table A6 ###
stargazer(list_model_partisan.1.5, 
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(0.05, 0.01, 0.001), no.space = T)

### Figure A3
preds.partisan.1 <- ggpredict(model.partisan.prog2.1, c("prog.vote.partisan.y[all]", "prog.pos.y"))

p1.partisan <- ggarrange(
  ggplot(preds.partisan.1)+
    geom_line(aes(x=x, y=predicted, color = group))+
    geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
    geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
    theme_minimal()+
    scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
    labs(color = "Party position \n in previous ballot")+
    xlab("partisan voters' support for conservative position")+
    ylab("Probability of conservative position")+
    theme(legend.position = "bottom"),
  ggplot(whole_data1)+
    geom_histogram(mapping = aes(x=prog.vote.partisan.y), alpha = 0.5)+
    labs(title = "Distribution of observations")+
    xlab("partisan voters' support for conservative position")+
    theme_minimal(),
  nrow=2
)

p1.partisan <- annotate_figure(p1.partisan, top = text_grob("One year lag", 
                                                            color = "black",size = 14))

preds.partisan.5 <- ggpredict(model.partisan.prog2.5, c("prog.vote.partisan.y[all]", "prog.pos.y"))

p5.partisan <- ggarrange(
  ggplot(preds.partisan.5)+
    geom_line(aes(x=x, y=predicted, color = group))+
    geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
    geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
    theme_minimal()+
    scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
    labs(color = "Party position \n in previous ballot")+
    xlab("partisan voters' support for conservative position")+
    ylab("Probability of conservative position")+
    theme(legend.position = "bottom"),
  ggplot(whole_data5)+
    geom_histogram(mapping = aes(x=prog.vote.partisan.y), alpha = 0.5)+
    labs(title = "Distribution of observations")+
    xlab("partisan voters' support for conservative position")+
    theme_minimal(),
  nrow=2
)

p5.partisan <- annotate_figure(p5.partisan, top = text_grob("Five year lag", 
                                                            color = "black",size = 14))

png("Figure/Figure A3.png", width=3000, height=2000, res=300)
ggarrange(p1.partisan, p5.partisan, ncol=2)
dev.off()


###### Relative partisan voter ######

### One year
model.relative.partisan.prog.1 <- glm(prog.pos.x ~ prog.pos.y + prog.vote.relative.partisan.y + prog.vote.median.y, data = whole_data1, family = binomial(link="logit"))

model.relative.partisan.prog2.1 <- glm(prog.pos.x ~ prog.pos.y * prog.vote.relative.partisan.y + prog.vote.median.y, data = whole_data1, family = binomial(link="logit"))

### Five year
model.relative.partisan.prog.5 <- glm(prog.pos.x ~ prog.pos.y + prog.vote.relative.partisan.y + prog.vote.median.y, data = whole_data5, family = binomial(link="logit"))

model.relative.partisan.prog2.5 <- glm(prog.pos.x ~ prog.pos.y * prog.vote.relative.partisan.y + prog.vote.median.y, data = whole_data5, family = binomial(link="logit"))

### List models 
list_model_relative.partisan.1.5 <- list(model.relative.partisan.prog.1, model.relative.partisan.prog2.1, model.relative.partisan.prog.5, model.relative.partisan.prog2.1)

### Table A7
stargazer(list_model_relative.partisan.1.5, 
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(0.05, 0.01, 0.001), no.space = T)


### Figure A4

preds.relative.partisan.1 <- ggpredict(model.relative.partisan.prog2.1, c("prog.vote.relative.partisan.y[all]", "prog.pos.y"))

p1.relative_partisan <- ggarrange(
  ggplot(preds.relative.partisan.1)+
    geom_line(aes(x=x, y=predicted, color = group))+
    geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
    geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
    theme_minimal()+
    scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
    labs(color = "Party position \n in previous ballot")+
    xlab("Relative partisan voters' support for conservative position")+
    ylab("Probability of conservative position")+
    theme(legend.position = "bottom"),
  ggplot(whole_data1)+
    geom_histogram(mapping = aes(x=prog.vote.relative.partisan.y), alpha = 0.5)+
    labs(title = "Distribution of observations")+
    xlab("Relative partisan voters' support for conservative position")+
    theme_minimal(),
  nrow=2
)

p1.relative_partisan <- annotate_figure(p1.relative_partisan, top = text_grob("One year lag", 
                                                                              color = "black",size = 14))


preds.relative.partisan.5 <- ggpredict(model.relative.partisan.prog2.5, c("prog.vote.relative.partisan.y[all]", "prog.pos.y"))

p5.relative_partisan <- ggarrange(
  ggplot(preds.relative.partisan.5)+
    geom_line(aes(x=x, y=predicted, color = group))+
    geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
    geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
    theme_minimal()+
    scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
    labs(color = "Party position \n in previous ballot")+
    xlab("Relative partisan voters' support for conservative position")+
    ylab("Probability of conservative position")+
    theme(legend.position = "bottom"),
  ggplot(whole_data5)+
    geom_histogram(mapping = aes(x=prog.vote.relative.partisan.y), alpha = 0.5)+
    labs(title = "Distribution of observations")+
    xlab("Relative partisan voters' support for conservative position")+
    theme_minimal(),
  nrow=2
)

p5.relative_partisan <- annotate_figure(p5.relative_partisan, top = text_grob("Five year lag", 
                                                                              color = "black",size = 14))

png("Figure/Figure A4.png", width=3000, height=2000, res=300)
ggarrange(p1.relative_partisan, p5.relative_partisan, ncol=2)
dev.off()


### models without the greens (nich parties)

### Median voters
model.median.prog_no_gps <- glm(prog.pos.x ~ prog.pos.y + prog.vote.median.y, data = whole_data3[whole_data3$party!="gps",], family = binomial(link="logit"))

model.median.prog2_no_gps <- glm(prog.pos.x ~ prog.pos.y * prog.vote.median.y, data = whole_data3[whole_data3$party!="gps",], family = binomial(link="logit"))

### Table A8
stargazer(model.median.prog_no_gps, model.median.prog2_no_gps, 
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(0.05, 0.01, 0.001), no.space = T)

### Figure A5
preds.median_no_gps <- ggpredict(model.median.prog2_no_gps, c("prog.vote.median.y[all]", "prog.pos.y"))

png("Figure/Figure A5.png", width=2500, height=3500, res = 300)
ggarrange(
  ggplot(preds.median_no_gps)+
    geom_line(aes(x=x, y=predicted, color = group))+
    geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
    geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
    theme_minimal()+
    scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
    labs(color = "Party position \n in previous ballot")+
    xlab("Median voters' support for conservative position")+
    ylab("Probability of conservative position")+
    theme(legend.position = "bottom"),
  ggplot(whole_data3[whole_data3$party!="gps",])+
    geom_histogram(mapping = aes(x=prog.vote.median.y), alpha = 0.5)+
    labs(title = "Distribution of observations")+
    xlab("Median voters' support for conservative position")+
    theme_minimal(),
  nrow=2
)
dev.off()


### Partisan voter
model.partisan.prog_no_gps <- glm(prog.pos.x ~ prog.pos.y + prog.vote.partisan.y, data = whole_data3[whole_data3$party!="gps",], family = binomial(link="logit"))

model.partisan.prog2_no_gps <- glm(prog.pos.x ~ prog.pos.y * prog.vote.partisan.y, data = whole_data3[whole_data3$party!="gps",], family = binomial(link="logit"))

### Table A9
stargazer(model.partisan.prog_no_gps, model.partisan.prog2_no_gps, 
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(0.05, 0.01, 0.001), no.space = T)

### Figure A6
preds.partisan_no_gps <- ggpredict(model.partisan.prog2_no_gps, c("prog.vote.partisan.y[all]", "prog.pos.y"))

png("Figure/Figure A6.png", width=2500, height=3500, res = 300)
ggarrange(
  ggplot(preds.partisan_no_gps)+
    geom_line(aes(x=x, y=predicted, color = group))+
    geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
    geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
    theme_minimal()+
    scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
    labs(color = "Party position \n in previous ballot")+
    xlab("partisan voters' support for conservative position")+
    ylab("Probability of conservative position")+
    theme(legend.position = "bottom"),
  ggplot(whole_data3[whole_data3$party!="gps",])+
    geom_histogram(mapping = aes(x=prog.vote.partisan.y), alpha = 0.5)+
    labs(title = "Distribution of observations")+
    xlab("partisan voters' support for conservative position")+
    theme_minimal(),
  nrow=2
)
dev.off()


### Relative partisan voter
model.relative.partisan.prog_no_gps <- glm(prog.pos.x ~ prog.pos.y + prog.vote.relative.partisan.y + prog.vote.median.y, data = whole_data3[whole_data3$party!="gps",], family = binomial(link="logit"))

model.relative.partisan.prog2_no_gps <- glm(prog.pos.x ~ prog.pos.y * prog.vote.relative.partisan.y + prog.vote.median.y, data = whole_data3[whole_data3$party!="gps",], family = binomial(link="logit"))

### Table A10
stargazer(model.relative.partisan.prog_no_gps, model.relative.partisan.prog2_no_gps, 
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(0.05, 0.01, 0.001), no.space = T)

### Figure A7
preds.relative.partisan_no_gps <- ggpredict(model.relative.partisan.prog2_no_gps, c("prog.vote.relative.partisan.y[all]", "prog.pos.y"))

png("Figure/Figure A7.png", width=2500, height=3500, res = 300)
ggarrange(
  ggplot(preds.relative.partisan_no_gps)+
    geom_line(aes(x=x, y=predicted, color = group))+
    geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
    geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
    theme_minimal()+
    scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
    labs(color = "Party position \n in previous ballot")+
    xlab("Relative partisan voters' support for conservative position")+
    ylab("Probability of conservative position")+
    theme(legend.position = "bottom"),
  ggplot(whole_data3[whole_data3$party!="gps",])+
    geom_histogram(mapping = aes(x=prog.vote.relative.partisan.y), alpha = 0.5)+
    labs(title = "Distribution of observations")+
    xlab("Relative partisan voters' support for conservative position")+
    theme_minimal(),
  nrow=2
)
dev.off()


#### Model without mandatory referendum

### Median voter
model.median.prog_no_mad_ref <- glm(prog.pos.x ~ prog.pos.y + prog.vote.median.y, data = whole_data3[whole_data3$rechtsform.x!=1 & whole_data3$rechtsform.y!=1,], family = binomial(link="logit"))

model.median.prog2_no_mad_ref <- glm(prog.pos.x ~ prog.pos.y * prog.vote.median.y, data = whole_data3[whole_data3$rechtsform.x!=1 & whole_data3$rechtsform.y!=1,], family = binomial(link="logit"))

### Table A11
stargazer(model.median.prog_no_mad_ref, model.median.prog2_no_mad_ref, 
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(0.05, 0.01, 0.001), no.space = T)

### Figure A8
preds.median_no_mad_ref <- ggpredict(model.median.prog2_no_mad_ref, c("prog.vote.median.y[all]", "prog.pos.y"))

png("Figure/Figure A8.png", width=2500, height=3500, res = 300)
ggarrange(
  ggplot(preds.median_no_mad_ref)+
    geom_line(aes(x=x, y=predicted, color = group))+
    geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
    geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
    theme_minimal()+
    scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
    labs(color = "Party position \n in previous ballot")+
    xlab("Median voters' support for conservative position")+
    ylab("Probability of conservative position")+
    theme(legend.position = "bottom"),
  ggplot(whole_data3[whole_data3$rechtsform.x!=1 & whole_data3$rechtsform.y!=1,])+
    geom_histogram(mapping = aes(x=prog.vote.median.y), alpha = 0.5)+
    labs(title = "Distribution of observations")+
    xlab("Median voters' support for conservative position")+
    theme_minimal(),
  nrow=2
)
dev.off()

### Partisan Voter
model.partisan.prog_no_mad_ref <- glm(prog.pos.x ~ prog.pos.y + prog.vote.partisan.y, data = whole_data3[whole_data3$rechtsform.x!=1 & whole_data3$rechtsform.y!=1,], family = binomial(link="logit"))

model.partisan.prog2_no_mad_ref <- glm(prog.pos.x ~ prog.pos.y * prog.vote.partisan.y, data = whole_data3[whole_data3$rechtsform.x!=1 & whole_data3$rechtsform.y!=1,], family = binomial(link="logit"))

### Table A12
stargazer(model.partisan.prog_no_mad_ref, model.partisan.prog2_no_mad_ref, 
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(0.05, 0.01, 0.001), no.space = T)

### Figure A9
preds.partisan_no_mad_ref <- ggpredict(model.partisan.prog2_no_mad_ref, c("prog.vote.partisan.y[all]", "prog.pos.y"))

png("Figure/Figure A9.png", width=2500, height=3500, res = 300)
ggarrange(
  ggplot(preds.partisan_no_mad_ref)+
    geom_line(aes(x=x, y=predicted, color = group))+
    geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
    geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
    theme_minimal()+
    scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
    labs(color = "Party position \n in previous ballot")+
    xlab("partisan voters' support for conservative position")+
    ylab("Probability of conservative position")+
    theme(legend.position = "bottom"),
  ggplot(whole_data3[whole_data3$rechtsform.x!=1 & whole_data3$rechtsform.y!=1,])+
    geom_histogram(mapping = aes(x=prog.vote.partisan.y), alpha = 0.5)+
    labs(title = "Distribution of observations")+
    xlab("partisan voters' support for conservative position")+
    theme_minimal(),
  nrow=2
)
dev.off()

### Relative partisan voter
model.relative.partisan.prog_no_mad_ref <- glm(prog.pos.x ~ prog.pos.y + prog.vote.relative.partisan.y + prog.vote.median.y, data = whole_data3[whole_data3$rechtsform.x!=1 & whole_data3$rechtsform.y!=1,], family = binomial(link="logit"))

model.relative.partisan.prog2_no_mad_ref <- glm(prog.pos.x ~ prog.pos.y * prog.vote.relative.partisan.y + prog.vote.median.y, data = whole_data3[whole_data3$rechtsform.x!=1 & whole_data3$rechtsform.y!=1,], family = binomial(link="logit"))

### Table A13
stargazer(model.relative.partisan.prog_no_mad_ref, model.relative.partisan.prog2_no_mad_ref, 
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(0.05, 0.01, 0.001), no.space = T)

### Figure A10
preds.relative.partisan_no_mad_ref <- ggpredict(model.relative.partisan.prog2_no_mad_ref, c("prog.vote.relative.partisan.y[all]", "prog.pos.y"))

png("Figure/Figure A10.png", width=2500, height=3500, res = 300)
ggarrange(
  ggplot(preds.relative.partisan_no_mad_ref)+
    geom_line(aes(x=x, y=predicted, color = group))+
    geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
    geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
    theme_minimal()+
    scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
    labs(color = "Party position \n in previous ballot")+
    xlab("Relative partisan voters' support for conservative position")+
    ylab("Probability of conservative position")+
    theme(legend.position = "bottom"),
  ggplot(whole_data3[whole_data3$rechtsform.x!=1 & whole_data3$rechtsform.y!=1,])+
    geom_histogram(mapping = aes(x=prog.vote.relative.partisan.y), alpha = 0.5)+
    labs(title = "Distribution of observations")+
    xlab("Relative partisan voters' support for conservative position")+
    theme_minimal(),
  nrow=2
)
dev.off()



###### Model with subsets of ballot with low and high saliency #####

swissvotes <- read.csv(url("https://swissvotes.ch/page/dataset/swissvotes_dataset.csv"), sep = ";")


swissvotes <- swissvotes[swissvotes$anr>=224 & ### After 1971
                           swissvotes$anr<=609 & ###Before 2017
                           swissvotes$rechtsform<=3, ## Only initivative, referenda mandatory and facultative
                         c(which(colnames(swissvotes)=="anr"),
                           which(colnames(swissvotes)=="bet"))]


swissvotes$anr.x <- swissvotes$anr
swissvotes$anr.y <- swissvotes$anr

swissvotes$bet.x <- swissvotes$bet
swissvotes$bet.y <- swissvotes$bet

whole_data3.b <- merge(whole_data3, swissvotes[,which(colnames(swissvotes) %in% c("anr.x", "bet.x"))], by = "anr.x")
whole_data3.b <- merge(whole_data3.b, swissvotes[,which(colnames(swissvotes) %in% c("anr.y", "bet.y"))], by = "anr.y")

whole_data3.b <- whole_data3.b %>% 
  mutate(bet.x=as.numeric(bet.x),
         bet.y=as.numeric(bet.y), 
         saliency.x = ifelse(bet.x<42.52, "low saliency",
                             ifelse(bet.x>=42.52, "high saliency", NA)),
         saliency.y = ifelse(bet.y<42.52, "low saliency",
                             ifelse(bet.y>=42.52, "high saliency", NA)))


### Median voter
### Low saliency
model.median.prog_sal_low <- glm(prog.pos.x ~ prog.pos.y + prog.vote.median.y, data = whole_data3.b[whole_data3.b$saliency.x=="low saliency" & whole_data3.b$saliency.y=="low saliency",], family = binomial(link="logit"))

model.median.prog2_sal_low <- glm(prog.pos.x ~ prog.pos.y * prog.vote.median.y, data = whole_data3.b[whole_data3.b$saliency.x=="low saliency" & whole_data3.b$saliency.y=="low saliency",], family = binomial(link="logit"))

### High saliency
model.median.prog_sal_high <- glm(prog.pos.x ~ prog.pos.y + prog.vote.median.y, data = whole_data3.b[whole_data3.b$saliency.x=="high saliency" & whole_data3.b$saliency.y=="high saliency",], family = binomial(link="logit"))

model.median.prog2_sal_high <- glm(prog.pos.x ~ prog.pos.y * prog.vote.median.y, data = whole_data3.b[whole_data3.b$saliency.x=="high saliency" & whole_data3.b$saliency.y=="high saliency",], family = binomial(link="logit"))

### Table A14
list_median_saliency <- list(model.median.prog_sal_low, model.median.prog2_sal_low, model.median.prog_sal_high, model.median.prog2_sal_high)

stargazer(list_median_saliency, 
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(0.05, 0.01, 0.001), no.space = T)

### Figure A11
preds.median_sal_low <- ggpredict(model.median.prog2_sal_low, c("prog.vote.median.y[all]", "prog.pos.y"))

preds.median_sal_high <- ggpredict(model.median.prog2_sal_high, c("prog.vote.median.y[all]", "prog.pos.y"))

png("Figure/Figure A11.png", width=3000, height=2000, res = 300)
ggarrange(
  ggarrange(
    ggplot(preds.median_sal_low)+
      geom_line(aes(x=x, y=predicted, color = group))+
      geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
      geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
      theme_minimal()+
      scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
      labs(color = "Party position \n in previous ballot", subtitle = "Low saliency")+
      xlab("Median voters' support for conservative position")+
      ylab("Probability of conservative position")+
      theme(legend.position = "bottom"),
    ggplot(whole_data3[whole_data3$rechtsform.x!=1 & whole_data3$rechtsform.y!=1,])+
      geom_histogram(mapping = aes(x=prog.vote.median.y), alpha = 0.5)+
      labs(title = "Distribution of observations")+
      xlab("Median voters' support for conservative position")+
      theme_minimal(),
    nrow=2
  ),
  ggarrange(
    ggplot(preds.median_sal_high)+
      geom_line(aes(x=x, y=predicted, color = group))+
      geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
      geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
      theme_minimal()+
      scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
      labs(color = "Party position \n in previous ballot", subtitle = "High saliency")+
      xlab("Median voters' support for conservative position")+
      ylab("Probability of conservative position")+
      theme(legend.position = "bottom"),
    ggplot(whole_data3[whole_data3$rechtsform.x!=1 & whole_data3$rechtsform.y!=1,])+
      geom_histogram(mapping = aes(x=prog.vote.median.y), alpha = 0.5)+
      labs(title = "Distribution of observations")+
      xlab("Median voters' support for conservative position")+
      theme_minimal(),
    nrow=2
  ), ncol = 2)
dev.off()


### Partisan voter
### Low saliency
model.partisan.prog_sal_low <- glm(prog.pos.x ~ prog.pos.y + prog.vote.partisan.y, data = whole_data3.b[whole_data3.b$saliency.x=="low saliency" & whole_data3.b$saliency.y=="low saliency",], family = binomial(link="logit"))

model.partisan.prog2_sal_low <- glm(prog.pos.x ~ prog.pos.y * prog.vote.partisan.y, data = whole_data3.b[whole_data3.b$saliency.x=="low saliency" & whole_data3.b$saliency.y=="low saliency",], family = binomial(link="logit"))

### High saliency
model.partisan.prog_sal_high <- glm(prog.pos.x ~ prog.pos.y + prog.vote.partisan.y, data = whole_data3.b[whole_data3.b$saliency.x=="high saliency" & whole_data3.b$saliency.y=="high saliency",], family = binomial(link="logit"))

model.partisan.prog2_sal_high <- glm(prog.pos.x ~ prog.pos.y * prog.vote.partisan.y, data = whole_data3.b[whole_data3.b$saliency.x=="high saliency" & whole_data3.b$saliency.y=="high saliency",], family = binomial(link="logit"))

### Table A15
list_partisan_saliency <- list(model.partisan.prog_sal_low, model.partisan.prog2_sal_low, model.partisan.prog_sal_high, model.partisan.prog2_sal_high)

stargazer(list_partisan_saliency, 
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(0.05, 0.01, 0.001), no.space = T)

### Figure A12
preds.partisan_sal_low <- ggpredict(model.partisan.prog2_sal_low, c("prog.vote.partisan.y[all]", "prog.pos.y"))

preds.partisan_sal_high <- ggpredict(model.partisan.prog2_sal_high, c("prog.vote.partisan.y[all]", "prog.pos.y"))


png("Figure/Figure A12.png", width=3000, height=2000, res = 300)
ggarrange(
  ggarrange(
    ggplot(preds.partisan_sal_low)+
      geom_line(aes(x=x, y=predicted, color = group))+
      geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
      geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
      theme_minimal()+
      scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
      labs(color = "Party position \n in previous ballot", subtitle = "Low saliency")+
      xlab("partisan voters' support for conservative position")+
      ylab("Probability of conservative position")+
      theme(legend.position = "bottom"),
    ggplot(whole_data3[whole_data3$rechtsform.x!=1 & whole_data3$rechtsform.y!=1,])+
      geom_histogram(mapping = aes(x=prog.vote.partisan.y), alpha = 0.5)+
      labs(title = "Distribution of observations")+
      xlab("partisan voters' support for conservative position")+
      theme_minimal(),
    nrow=2
  ),
  ggarrange(
    ggplot(preds.partisan_sal_high)+
      geom_line(aes(x=x, y=predicted, color = group))+
      geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
      geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
      theme_minimal()+
      scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
      labs(color = "Party position \n in previous ballot", subtitle = "High saliency")+
      xlab("partisan voters' support for conservative position")+
      ylab("Probability of conservative position")+
      theme(legend.position = "bottom"),
    ggplot(whole_data3[whole_data3$rechtsform.x!=1 & whole_data3$rechtsform.y!=1,])+
      geom_histogram(mapping = aes(x=prog.vote.partisan.y), alpha = 0.5)+
      labs(title = "Distribution of observations")+
      xlab("partisan voters' support for conservative position")+
      theme_minimal(),
    nrow=2
  ), ncol=2)

dev.off()

### Relative Partisan voter
### Low saliency
model.relative.partisan.prog_sal_low <- glm(prog.pos.x ~ prog.pos.y + prog.vote.relative.partisan.y + prog.vote.median.y, data = whole_data3.b[whole_data3.b$saliency.x=="low saliency" & whole_data3.b$saliency.y=="low saliency",], family = binomial(link="logit"))

model.relative.partisan.prog2_sal_low <- glm(prog.pos.x ~ prog.pos.y * prog.vote.relative.partisan.y + prog.vote.median.y, data = whole_data3.b[whole_data3.b$saliency.x=="low saliency" & whole_data3.b$saliency.y=="low saliency",], family = binomial(link="logit"))

### High saliency
model.relative.partisan.prog_sal_high <- glm(prog.pos.x ~ prog.pos.y + prog.vote.relative.partisan.y + prog.vote.median.y, data = whole_data3.b[whole_data3.b$saliency.x=="high saliency" & whole_data3.b$saliency.y=="high saliency",], family = binomial(link="logit"))

model.relative.partisan.prog2_sal_high <- glm(prog.pos.x ~ prog.pos.y * prog.vote.relative.partisan.y + prog.vote.median.y, data = whole_data3.b[whole_data3.b$saliency.x=="high saliency" & whole_data3.b$saliency.y=="high saliency",], family = binomial(link="logit"))


### Table A16
list_relaive_partisan_saliency <- list(model.relative.partisan.prog_sal_low, model.relative.partisan.prog2_sal_low, model.relative.partisan.prog_sal_high, model.relative.partisan.prog2_sal_high)

stargazer(list_relaive_partisan_saliency, 
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(0.05, 0.01, 0.001), no.space = T)

### Figure A13
preds.relative.partisan_sal_low <- ggpredict(model.relative.partisan.prog2_sal_low, c("prog.vote.relative.partisan.y[all]", "prog.pos.y"))

preds.relative.partisan_sal_high <- ggpredict(model.relative.partisan.prog2_sal_high, c("prog.vote.relative.partisan.y[all]", "prog.pos.y"))

png("Figure/Figure A13.png", width=3000, height=2000, res = 300)
ggarrange(
  ggarrange(
    ggplot(preds.relative.partisan_sal_low)+
      geom_line(aes(x=x, y=predicted, color = group))+
      geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
      geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
      theme_minimal()+
      scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
      labs(color = "Party position \n in previous ballot", subtitle = "Low saliency")+
      xlab("Relative partisan voters' support for conservative position")+
      ylab("Probability of conservative position")+
      theme(legend.position = "bottom"),
    ggplot(whole_data3[whole_data3$rechtsform.x!=1 & whole_data3$rechtsform.y!=1,])+
      geom_histogram(mapping = aes(x=prog.vote.relative.partisan.y), alpha = 0.5)+
      labs(title = "Distribution of observations")+
      xlab("Relative partisan voters' support for conservative position")+
      theme_minimal(),
    nrow=2
  ),
  ggarrange(
    ggplot(preds.relative.partisan_sal_high)+
      geom_line(aes(x=x, y=predicted, color = group))+
      geom_line(aes(x=x, y=conf.low, color = group), size = 0.3)+
      geom_line(aes(x=x, y=conf.high, color = group), size = 0.3)+
      theme_minimal()+
      scale_color_manual(values = c("blue", "red"), labels = c("Progressive", "Conservative"))+
      labs(color = "Party position \n in previous ballot", subtitle = "High saliency")+
      xlab("Relative partisan voters' support for conservative position")+
      ylab("Probability of conservative position")+
      theme(legend.position = "bottom"),
    ggplot(whole_data3[whole_data3$rechtsform.x!=1 & whole_data3$rechtsform.y!=1,])+
      geom_histogram(mapping = aes(x=prog.vote.relative.partisan.y), alpha = 0.5)+
      labs(title = "Distribution of observations")+
      xlab("Relative partisan voters' support for conservative position")+
      theme_minimal(),
    nrow=2
  ), ncol=2)

dev.off()


######## Appendix B ########

png("Figure/Figure B1.png", width=2000, height=2000, res=300)
stan_rhat(model)
dev.off()

png("Figure/Figure B2.png", width=2000, height=2000, res=300)
stan_ess(model)
dev.off()

png("Figure/Figure B3.png", width=2000, height=3000, res=300)
stan_diag(model, info = 'sample')
dev.off()

######## Appendix C #######

png("Figure/Figure C1.png", width = 3500, height = 2000, res=300)
ggplot(thetas)+
  geom_line(aes(x=t+1970, y=summary.50., color = factor(j)))+
  geom_line(aes(x=t+1970, y=summary.10., color = factor(j)), size = 0.1)+
  geom_line(aes(x=t+1970, y=summary.90., color = factor(j)), size = 0.1)+
  xlab("Years")+
  ylab(expression(theta[j][t]))+
  scale_color_manual(name = "Party", 
                     labels = c("CVP", "FDP", "GPS", "SPS", "SVP"), 
                     values = c("orange", "blue", "green", "red", "dark green"))+
  theme_minimal()+
  theme(legend.position = "bottom")

dev.off()

